package com.example.easybuy.service.impl;

import com.example.easybuy.pojo.EasybuyProduct;
import com.example.easybuy.pojo.EasybuyUser;
import com.example.easybuy.pojo.EasybuyUserAddress;
import com.example.easybuy.pojo.Product;
import com.example.easybuy.service.BuyCarService;
import com.example.easybuy.service.EasybuyProductService;
import com.example.easybuy.service.EasybuyUserAddressService;
import com.example.easybuy.service.EasybuyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BuyCarServiceImpl implements BuyCarService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    EasybuyProductService productService;

    @Autowired
    EasybuyUserService easybuyUserService;

    @Autowired
    EasybuyUserAddressService easybuyUserAddressService;

    @Override
    public boolean addProductToBuyCar(Integer userId, Integer productId, Integer addNum) {
        //添加商品进购物车

        //获取Redis中的购物车
        Map<String, Object> redisMap = (Map<String, Object>) redisTemplate.opsForValue().get("car" + userId);
        Map<String, Integer> stockMap = new HashMap<>();
        List<EasybuyProduct> productList = new ArrayList<>();
        boolean flag = true;
        if (redisMap != null) {
            //获取对应商品的数量 Map<productId + "",num> 商品Id为key 选购数量为value
            stockMap = (Map<String, Integer>) redisMap.get("stockMap");
            //获取购物车中商品
            productList = (List<EasybuyProduct>) redisMap.get("productList");
            for (EasybuyProduct product : productList) {
                if (product.getId().equals(productId)) {   //如果购物车已有此商品 添加该商品的数量
                    for (String key : stockMap.keySet()) {
                        if (key.equals(productId + "")) {
                            stockMap.put(key, stockMap.get(key) + addNum);
                            flag = false;
                            break;
                        }
                    }
                    break;
                }
            }
            if (flag) {  //true 没有此商品 false 有此商品  已添加该商品的数量
                productList.add(productService.getProductById(productId));
                stockMap.put(String.valueOf(productId), addNum);
            }
        } else {
            redisMap = new HashMap<>();
            stockMap.put(String.valueOf(productId), addNum);
            productList.add(productService.getProductById(productId));
        }
        //覆盖redisMap中的数据
        redisMap.put("stockMap", stockMap);
        redisMap.put("productList", productList);
        //将数据传入Redis
        redisTemplate.opsForValue().set("car" + userId, redisMap);
        return true;
    }

    @Override
    public boolean reduceProduct(Integer userId, Integer productId, Integer num) {
        return false;
    }

    @Override
    public boolean delProduct(Integer userId, Integer productId) {
        //获取Redis中的购物车
        Map<String, Object> redisMap = (Map<String, Object>) redisTemplate.opsForValue().get("car" + userId);
        //获取对应商品的数量 Map<productId + "",num> 商品Id为key 选购数量为value
        Map<String, Integer> stockMap = (Map<String, Integer>) redisMap.get("stockMap");
        //获取购物车中商品
        List<EasybuyProduct> productList = (List<EasybuyProduct>) redisMap.get("productList");

        for (EasybuyProduct product : productList) {
            if (product.getId().equals(productId)) {     //如果购物车已有此商品 删除该商品及数量
                for (String key : stockMap.keySet()) {
                    if (key.equals(productId.toString())) {
                        stockMap.remove(key);
                        break;
                    }
                }
                productList.remove(product);
                break;
            }
        }
        //覆盖redisMap中的数据
            redisMap.put("stockMap", stockMap);
            redisMap.put("productList", productList);
            redisTemplate.opsForValue().set("car" + userId, redisMap);
         if(stockMap.size()==0&&productList.size()==0){
             redisTemplate.delete("car" + userId);
         }
        //将数据传入Redis
        return true;
    }

    @Override
    public Map<String, Object> getBuyCar(Integer userId) {
        return (Map<String, Object>) redisTemplate.opsForValue().get("car" + userId);
    }

    @Override
    public Map<String, Object> createViewForOrderMap(Integer userId) {
        //获取Redis中的购物车
        Map<String, Object> redisMap = (Map<String, Object>) redisTemplate.opsForValue().get("car" + userId);
        redisMap.remove("car" + userId);
        //往购物车Map中添加数据 用户地址userAddressList 用户对象
        EasybuyUser user = easybuyUserService.selectUserById(userId);
        redisMap.put("user", user);
        List<EasybuyUserAddress> userAddressList = easybuyUserAddressService.getAddressByUserId(userId);
        redisMap.put("userAddressList", userAddressList);

        return redisMap;
    }

    @Override
    public String createOrder(Integer userId, String address) {
        return null;
    }

    @Override
    public boolean updateProductToBuyCar(Integer userId, Integer productId, Integer num) {
        return false;
    }
}
